package com.cgbl.cg.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppFragmentClassificationBinding
import com.cgbl.cg.ui.classification.ClassificationTabFragment
import com.cgbl.cg.ui.home.LimitedTimeFragment
import com.cgbl.cg.vm.main.ClassificationFragmentViewModel
import com.robot.base.base.activity.GLBaseFragment
import me.goldze.mvvmhabit.BR

class ClassificationFragment :
    GLBaseFragment<AppFragmentClassificationBinding, ClassificationFragmentViewModel>() {
    private var fragmentList = mutableListOf<Fragment>()

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_classification

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        fragmentList.add(ClassificationTabFragment.newInstance("0"))
        fragmentList.add(ClassificationTabFragment.newInstance("1"))
        fragmentList.add(ClassificationTabFragment.newInstance("2"))
        fragmentList.add(ClassificationTabFragment.newInstance("3"))
        binding.mViewPager2.adapter = object : FragmentStatePagerAdapter(childFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }
        binding.mViewPager2.offscreenPageLimit = fragmentList.size
        binding.mViewPager2.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                when(position){
                    0->{
                        viewModel.clickSelectVideo.execute()
                    }
                    1->{
                        viewModel.clickSelectResource.execute()
                    }
                    2->{
                        viewModel.clickSelectWork.execute()
                    }
                    else->{
                        viewModel.clickSelectProject.execute()
                    }
                }
            }

        })
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.tabType.observe(this){
            binding.mViewPager2.currentItem = it
        }
    }
}